﻿using UnityEngine;
using System.Collections;

public class FlightSystem : MonoBehaviour
{
    public Camera camera = null;
    public Transform aircraft = null;

	// Use this for initialization
	void Start () 
    {
        Quaternion airQuaternion = aircraft.rotation;

        zAngle = airQuaternion.z;
        xAngle = airQuaternion.x;
        yAngle = airQuaternion.y;
	}

    void FixedUpdate()
    {

    }

    public enum FLIGHT_STATE
    {
        ACCELERATE ,
        DECELERATE ,
        CONSTANT 
    }

    public FLIGHT_STATE flightState = FLIGHT_STATE.CONSTANT;

	// Update is called once per frame
    public float flightSpeed = 30.0f; //飞行速度
    public const float SPEED_MAX = 1000.0f;
    public const float SPEED_MIN = 30.0f;
    public float maxAcceleration = 100.0f;
    public float maxDeceleration = 100.0f;
    public float miniDeceleration = 10.0f;
    public float duration = 0.0f;
    public float toMaxSpeedSec = 30.0f;
    public float toNormalSpeedSec = 1.3f;
    private float curAcceleration = 0.0f;
    

    private Vector3 normalPos = new Vector3(0, 15, -30);
    private Vector3 acceleratePos = new Vector3(0, 15, -40);
    private Vector3 targetPos = Vector3.up;

    private float smoothTime = 0.3f;
    private Vector3 currentVelocity = Vector3.zero; //射线机平滑阻尼时候的速度

    public float zAngle = 0.0f;
    public float xAngle = 0.0f;
    public float yAngle = 0.0f;
    public float roundRadius = 100.0f;
	void Update () 
    {
        float angleAdded = 100.0f * Time.deltaTime;

        flightState = FLIGHT_STATE.CONSTANT;
        if( Input.GetKey(KeyCode.W) ) //w s 只能同时选择一个按钮
        {
            //flightState = FLIGHT_STATE.ACCELERATE;


            xAngle += angleAdded ;
        }
        else if(Input.GetKey(KeyCode.S))
        {
            //flightState = FLIGHT_STATE.DECELERATE;


            xAngle -= angleAdded;
        }


        
        if( Input.GetKey(KeyCode.A)) //a d只能同时选择一个
        {
            yAngle -= angleAdded; 
        }
        else if( Input.GetKey(KeyCode.D))
        {
            yAngle += angleAdded;  
        }

        if (Input.GetAxis("Mouse X") != 0)
        {
            zAngle += Input.GetAxis("Mouse X") * 3.8f;
        }

        //if( Input.GetAxis("Mouse Y") != 0 )
        //{
        //    xAngle += Input.GetAxis("Mouse Y") * 10.0f;
        //}

        if (flightState == FLIGHT_STATE.CONSTANT)
        {
            flightSpeed -= miniDeceleration * Time.deltaTime;
            flightSpeed = flightSpeed < SPEED_MIN ? SPEED_MIN : flightSpeed;

            targetPos = normalPos ;
            smoothTime = 0.2f;
        }
        else if (flightState == FLIGHT_STATE.ACCELERATE)
        {
            flightSpeed += maxAcceleration * Time.deltaTime;
            flightSpeed = flightSpeed > SPEED_MAX ? SPEED_MAX : flightSpeed;

             targetPos = acceleratePos ;
             smoothTime = 0.2f;
        }
        else if (flightState == FLIGHT_STATE.DECELERATE)
        {
            flightSpeed -= maxDeceleration * Time.deltaTime;
            flightSpeed = flightSpeed < SPEED_MIN ? SPEED_MIN : flightSpeed;

            targetPos = normalPos ;
            smoothTime = 0.1f;
        }


        //aircraft.Rotate(Vector3.up, yAngle);
        //aircraft.rotation = Quaternion.Euler( new VecQuaternion.Euler(xAngle, yAngle, zAngle);tor3(0,0,zAngle) );
        aircraft.rotation = Quaternion.Euler(xAngle, yAngle, zAngle);

        aircraft.Translate(Vector3.forward * Time.deltaTime * flightSpeed);
        camera.transform.localPosition = Vector3.SmoothDamp(camera.transform.localPosition, targetPos, ref currentVelocity, smoothTime);
	}
}
